package de.tum.in.eist.poll.client.gui;

import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.visualization.client.AbstractDataTable;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.LegendPosition;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.visualizations.ColumnChart;
import com.google.gwt.visualization.client.visualizations.ColumnChart.Options;

public class LectureChartWidget extends VerticalPanel {

	private ColumnChart chart;
	private HorizontalPanel legend;
	private Label[] color;
	private HorizontalPanel[] pack;
	
	public LectureChartWidget(int[] stats, String title) {
		init(stats, title);
		addContent();
	}

	private void addContent() {
		add(chart);		
		add(legend);
	}

	private void init(int[] stats, String title) {
		chart = new ColumnChart(createTable(stats), createOptions(title));
		legend = new HorizontalPanel();	
		color = new Label[LectureVotePanel.VOTES];
		pack = new HorizontalPanel[LectureVotePanel.VOTES];
		
		// Add a legend with smiles and tooltips
		for (int i = 0; i < LectureVotePanel.VOTES; i++) {
			pack[i] = new HorizontalPanel();
			pack[i].addStyleName("chartLegendElement");
			
			// Colors
			color[i] = new Label();
			color[i].setSize("15px", "15px");
			color[i].addStyleName("chartLegendColor" +i);	
			// Smiley
			Image image = new Image(LectureVotePanel.SMILEY[i]);
			image.setTitle(LectureVotePanel.ANSWER[i]);
			
			pack[i].add(color[i]);
			pack[i].add(image);
			legend.add(pack[i]);
		}
		
		// Add styles
		chart.addStyleName("chartForm");
		legend.addStyleName("chartLegendForm");
		addStyleName("chartForm");
	}
	
	private static Options createOptions(String title) {
		Options options = Options.create();
		options.setLegend(LegendPosition.NONE);
		// options.setLegendFontSize(12.0);
		// options.setLegendBackgroundColor("transparent");
		options.setWidth(540);
		options.setHeight(250);
		options.setTitle(title);
		options.setColors("green", "transparent", "greenyellow", "transparent",
				"yellow", "transparent", "orange", "transparent", "red");
		options.setBackgroundColor("transparent");
		options.setEnableTooltip(true);
		options.setTooltipHeight(50);
		options.setTooltipWidth(100);
		return options;
	}

	private static AbstractDataTable createTable(int[] stats) {
		DataTable data = DataTable.create();
		data.addColumn(ColumnType.NUMBER, "Very satisfied");
		data.addColumn(ColumnType.STRING, "");
		data.addColumn(ColumnType.NUMBER, "Satisfied");
		data.addColumn(ColumnType.STRING, "");
		data.addColumn(ColumnType.NUMBER, "Neutral");
		data.addColumn(ColumnType.STRING, "");
		data.addColumn(ColumnType.NUMBER, "Unsatisfied");
		data.addColumn(ColumnType.STRING, "");
		data.addColumn(ColumnType.NUMBER, "Very unsatisfied");
		data.addRows(1);
		for (int i = 0; i < LectureVotePanel.VOTES; i++) {
			data.setValue(0, i + i, stats[i]);
		}
		return data;
	}

	public LectureChartWidget(int[] stats) {
		this(stats, "");
	}

	public LectureChartWidget() {
		this(new int[5], "");
	}

	public void update(int[] stats, String title) {
		chart.draw(createTable(stats), createOptions(title));
	}
}
